package com.situ.meetingRoom.service.impl;

import com.situ.meetingRoom.dao.DeptDao;
import com.situ.meetingRoom.dao.UserDao;
import com.situ.meetingRoom.dao.impl.DeptDaoImpl;
import com.situ.meetingRoom.dao.impl.UserDaoImpl;
import com.situ.meetingRoom.entity.Dept;
import com.situ.meetingRoom.entity.User;
import com.situ.meetingRoom.service.DeptService;
import com.situ.meetingRoom.util.PageInfo;

import java.util.List;

public class DeptServiceImpl implements DeptService {
    private DeptDao deptDao= new DeptDaoImpl();
    @Override
    public List<Dept> getAll(Dept dept) {
        return deptDao.selectAll(dept);
    }

    @Override
    public PageInfo getForPage(Integer page, Integer limit, Dept dept) {
        return deptDao.selectForPage(page,limit ,dept);
    }

    @Override
    public int add(Dept dept) throws Exception {
        //        保证那唯一的字段不能重复
        //        保存到数据库
        return deptDao.insert(dept);
    }

    @Override
    public int remove(Integer id) throws Exception {
        Dept dept = deptDao.selectById(id);
        //部门不存在
        UserDao ud = new UserDaoImpl();
        List<User> list = ud.selectDeptId(id);
        if (!list.isEmpty()){
            throw new Exception("该部门尚有员工在职");
        }
        if (dept == null) {
            throw new Exception("员工不存在");
        }
        //员工状态
        if (dept.getStatus() == 1) {
            throw new Exception("部门已裁撤");
        }
        //设置员工状态
        dept.setStatus(1);
        //更新到数据库
        return deptDao.update(dept);
    }

    @Override
    public int edit(Dept dept) throws Exception {
        //有效性 电话不能冲突
        //部门ID验证
        return deptDao.update(dept);
    }
    @Override
    public Dept getById(Integer id) {
        return deptDao.selectById(id);
    }
}
